﻿using Sunny.UI;
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Forms;
using DAL;
using Coomtools.dbhelper;
using CYLScheck.UIForms;
using CYLScheck.UIcomm;
using System.Threading;

namespace CYLScheck
{
    /// <summary>
    /// 关闭主窗体方法的委托类型
    /// </summary>
    public delegate void Declosedmainform();
    
    public partial class MainFrm : UIHeaderAsideMainFooterFrame
    {
        /// <summary>
        /// 全局用户使用的主题类类型 解决子窗口没有加到内存时 主题管理不生效的问题
        /// </summary>
  
        Declosedmainform CLoseMf;
        //用于记录快捷打开窗口page页的 pageindex值 通过SelectPage 方法打开对应的窗口
       
        public MainFrm()
        {
            InitializeComponent();

            timer1.Start();
           blackPageFrm blackpagefrm = new blackPageFrm();
            AddPage(blackpagefrm, 9999);
            SelectPage(9999);

            // 主窗体框架样式加载。。。。。。。。。。。。。。。。。。。。。。。。。。。。。。。。


            //Header.SetNodePageIndex(Header.Nodes[0], pageIndex);
            //Header.SetNodeSymbol(Header.Nodes[0], 61451);
            //通过代码添加左边导航栏项 和 其子项目 也可以用设计器添加 
            //******************创建左边栏 的 第一个 菜单何其 两个子菜单******************
            //******** int sybole 61451 代表资源图标号码 sybomesize 24 代表图标大小********

            //int pageIndex1 = 1000;
            //TreeNode parent1 = Aside.CreateNode("功能大类别1", 61451, 24, pageIndex1);

            //通过设置PageIndex关联，节点文字、图标由相应的Page的Text、Symbol提供

            //ChidPageFrm11 ChpageFrm1 = new ChidPageFrm11();
            //Aside.CreateChildNode(parent1, AddPage(ChpageFrm1, ++pageIndex1));

            //Quick_usePageindex1 = pageIndex1;

            //Aside.CreateChildNode(parent1, AddPage(new ChidPageFrm12(), ++pageIndex1));

            //*****************************************************
            //**************创建左边栏 的 第二个 菜单何其 两个子菜单******************
            //  int pageIndex2= 2000;
            //  TreeNode parent2 = Aside.CreateNode("功能大类别2", 61818, 24, pageIndex2);
            //Aside.CreateChildNode(parent2, AddPage(new ChidPageFrm21(), ++pageIndex2));
            //  Quick_usePageindex2 = pageIndex2;
            //  Aside.CreateChildNode(parent2, AddPage(new ChidPageFrm22(), ++pageIndex2));

            //************************创建左边栏 的 第N个 菜单何其 N个子菜单******

            //*******************************************************
            //**********增加单独页面不在 aside和heard 里面 通过 按钮  调出的页面**********




            //**********************************************************
        }
        /// <summary>
        /// 程序启动前  初始化并检查数据库
        /// </summary>
        private void BeginStart_loadini()
        {
       //启动前 读取 loginset.ini 文件中的配置信息
            if (loadsetting.load_databaseconstring()==false)
            {
                this.Close();
                return;
            }
        



        }
    
      
    /// <summary>
    /// 在时钟线程中检查数据库连接是否正常
    /// </summary>
        private void timer_checkdatabase()
        {
            //尝试 连接数据库是否成功


            Checkdatabase Check_dbcon = new Checkdatabase();

            if (Check_dbcon.Check_databaseLink() == false)
            {
                    SymLabe_link.BeginInvoke(new Action(() =>
                    {
                        SymLabe_link.AutoSize = true;
                        SymLabe_link.Text = "状态: " + Gobleset.link_databasename + " 连接失败";
                        SymLabe_link.Symbol = 61453;
                        SymLabe_link.SymbolColor = Color.Red;

                    }));
               
            }
            else
            {
                SymLabe_link.BeginInvoke(new Action(() =>
                {
                    SymLabe_link.AutoSize = true;
                    SymLabe_link.Text = "状态: " + Gobleset.link_databasename + " 连接成功";
                    SymLabe_link.Symbol = 61452;
                    SymLabe_link.SymbolColor = Color.LightGreen;
                }));

            }
        }

        /// <summary>
        /// 主窗口显示前 检查数据库连接状态并显示 连接状态
        /// </summary>
        private void BeginStart_checkdatabase()
        {
            //尝试 连接数据库是否成功
         
           
            Checkdatabase Check_dbcon = new Checkdatabase();
            
            if (Check_dbcon.Check_databaseLink() == false)
            {
                HideWaitForm();
            

                if (UIMessageDialog.ShowMessageDialog(new Form(), "工作数据库连接失败！是否继续运行！", "数据库连接错误", true, UIStyle.Orange) == false)
                {
                    //要处理跨线程访问问题
                    CLoseMf();
                    return;
                }

                else
                {
                    SymLabe_link.BeginInvoke(new Action(() =>
                    {
                        SymLabe_link.AutoSize = true;
                        SymLabe_link.Text = "状态: " + Gobleset.link_databasename + " 连接失败";
                    SymLabe_link.Symbol = 61453;
                    SymLabe_link.SymbolColor = Color.Red;

                    }));
                   
                }
            }
            else
            {
                HideWaitForm();
                SymLabe_link.BeginInvoke(new Action(() =>
                {
                    SymLabe_link.AutoSize = true;
                    SymLabe_link.Text = "状态: " + Gobleset.link_databasename + " 连接成功";
                SymLabe_link.Symbol = 61452;
                SymLabe_link.SymbolColor = Color.LightGreen;
                }));
                
            }

          

        }

/// <summary>
/// 检查是否是注册用户
/// </summary>
        private void BeginStart_checkReg()

        {
            if (loadsetting.Check_IsReguser() == true && Gobleset.Gobel_IsRegUser=="YES"  )
            {
                // 代表是注册用户下面将 各个功能分别开启——————————————————————————————————

                this.Text = this.Text + "(注册用户)";




                //_________________开启功能结束——————————————————————————————-——————



                return;
            }
            else
            {
                this.Text = this.Text + "(共享版用户)";
            }
        }


      /// <summary>
      /// 读取用户主题类型
      /// </summary>
      /// <returns>读取到的主题类型</returns>
        public UIStyle  loadUserUI()

        {
            UIStyle style;
            switch (Gobleset.userUI)
            {


                case 0:
                    style = UIStyle.Custom;
                    break;
                case 1:
                    style =UIStyle.Blue ;
                    break;
                case 2:
                    style =UIStyle.Green ;

                    break;
                case 3:
                    style = UIStyle.Orange;
                    break;
                case 4:
                    style =UIStyle.Red ;

                    break;
                case 5:
                    style =UIStyle.Gray ;

                    break;
                case 6:
                    style =UIStyle.Purple ;

                    break;
                case 7:
                    style = UIStyle.LayuiGreen;

                    break;
                case 8:
                    style =UIStyle.LayuiRed ;

                    break;
                case 9:
                    style =UIStyle.LayuiOrange ;

                    break;
                case 101:
                    style =UIStyle.DarkBlue ;

                    break;
                case 102:
                    style =UIStyle.Black ;

                    break;
                case 999:
                    style =UIStyle.Colorful ;

                    break;
                default:
                    style = UIStyle.Orange;
                      break;
                   }
           
          return style;  

        }


        /// <summary>
        /// 对不是注册用户关闭各项功能
        /// </summary>
        private void Begin_NOuserShutdown()

        { }
      

        private void MainFrm_Load(object sender, EventArgs e)
        {
             //关闭非注册用户的各项功能
            Begin_NOuserShutdown();
           
            //______________开始 先加载配置 文件 和 用与控制主窗体的 委托回调____________________________ 
           
            BeginStart_loadini();

          

            //开始检查是否是注册用户
            BeginStart_checkReg();

            
            //挂载 关闭主窗体的委托回调  通过参数形式 传入子窗体的构造函数中
            
            CLoseMf += ColsedMainFrm;

            //______________结束载配置 文件 和 用与控制主窗体的 委托回调____________________________ 

            ///判读 登录界面账户是否启用 不启用就不显示登录窗体
            if (Gobleset.enableuser ==true)
            {
             LoginFrm LgForm = new LoginFrm(CLoseMf);
            LgForm.ShowDialog();
            this.Hide();
            }
            //************************************

           
            
            
            
            Gobleset.Gobel_userstyle = loadUserUI();
            uiStyleManager.Style = Gobleset.Gobel_userstyle;
       



        }
          /// <summary>
          /// 主界面显示前做一些检查或初始化
          /// </summary>
          /// <param name="sender"></param>
          /// <param name="e"></param>
        private void MainFrm_Shown(object sender, EventArgs e)
        {

            //  BeginStart_checkdatabase();


            ///开启数据库是否有连接的检查线程
            ///Thread dbtest = new Thread(new ThreadStart(BeginStart_checkdatabase)) ;

            Thread dbtest = new Thread(BeginStart_checkdatabase);
            dbtest.IsBackground = true;
             dbtest.Start();
            ShowWaitForm("数据库 " + Gobleset.link_databasename + " 连接检查中.....");
            dbtest.Join();
            //dbtesttimer.Interval = 10000;
            //dbtesttimer.Start();
           






        }

       /// <summary>
       ///头部快捷窗口实现 快捷打开对应窗体
       /// </summary>
       /// <param name="itemText"></param>
       /// <param name="menuIndex">选择的第几个菜单</param>
       /// <param name="pageIndex"></param>
        private void Header_MenuItemClick(string itemText, int menuIndex, int pageIndex)
        {

            switch (menuIndex)
            {
                case 0:
                    UIPage chldPage11 = GetPage<ChidPageFrm11>();
                    if (chldPage11 != null)
                        SelectPage(chldPage11.PageIndex);
                    else
                    {
                       
                        ChidPageFrm11 childpagefrm11 = new ChidPageFrm11();
                        AddPage(childpagefrm11, 1001);
                        SelectPage(1001);
                    }

                        break;
                case 1:
                   

                    UIPage chldPage12 = GetPage<ChidPageFrm12>();
                    if (chldPage12 != null)
                        SelectPage(chldPage12.PageIndex);
                    else
                    {
                        
                        ChidPageFrm12 childpagefrm12 = new ChidPageFrm12();
                        AddPage(childpagefrm12,1002);
                        SelectPage(1002);
                     
                    }
                     break;

                case 2:


                    UIPage chldPage21 = GetPage<ChidPageFrm21>();
                    if (chldPage21 != null)
                        SelectPage(chldPage21.PageIndex);
                    else
                    {

                        ChidPageFrm21 childpagefrm21 = new ChidPageFrm21();
                        AddPage(childpagefrm21, 2001);
                        SelectPage(2001);
                    }
                    break;


                case 3:


                    UIPage chldPage22 = GetPage<ChidPageFrm22>();
                    if (chldPage22 != null)
                        SelectPage(chldPage22.PageIndex);
                    else
                    {

                        ChidPageFrm22 childpagefrm22 = new ChidPageFrm22();
                        AddPage(childpagefrm22, 2002);
                        SelectPage(2002);
                    }
                    break;

                default:
                    //Aside.SelectPage(Quick_usePageindex1);
                    break;
            }

        }

       /// <summary>
       /// 右下角时钟显示
       /// </summary>
       /// <param name="sender"></param>
       /// <param name="e"></param>
        private void timer1_Tick(object sender, EventArgs e)
        {
            DateTime tm = DateTime.Now;
            string tim = tm.ToString("HH:mm:ss");
            SymLabel_time.Text = tim;

        }

        private void symbolbtton_seting_Click(object sender, EventArgs e)
        {
            RemoveAllPage();
            AddPage(new PageSettingFrm(CLoseMf),9901);
            SelectPage(9901);
            //uiStyleManager.Style = Gobleset.Gobel_userstyle;



        }
/// <summary>
/// 关闭主窗体
/// </summary>
        public   void  ColsedMainFrm()
        {

            this.BeginInvoke(new Action(() =>
            {
                //this.Disabled();
                //this.Close();
                Application.Exit();

            }));
           
        }
        private void uiSButton_Closed_Click(object sender, EventArgs e)
        {

            ColsedMainFrm();
        } 

   
         ////左边栏 功能打开对应窗体实现
        private void Aside_MenuItemClick(TreeNode node, NavMenuItem item, int pageIndex)
        {

       

            switch (node.Name)
            {
                case "Categories1Frm1":
                    RemoveAllPage();
                    ChidPageFrm11 childpagefrm11 = new ChidPageFrm11();
                    AddPage(childpagefrm11, 1001);
                    SelectPage(1001);
                    #region 保存当前页面看是否必要在修改 像快捷窗体一样不清空所有 uipage当都所有页面都打开没意义
                    //   UIPage chldPage11 = GetPage<ChidPageFrm11>();
                    //   if (chldPage11 != null)
                    //   {

                    //SelectPage(chldPage11.PageIndex);

                    //   }

                    //   else
                    //   {
                    //       RemoveAllPage();
                    //       ChidPageFrm11 childpagefrm11 = new ChidPageFrm11();
                    //       AddPage(childpagefrm11, 1001);
                    //       SelectPage(1001);

                    //   }
                    #endregion
  

                    break;
                case "Categories1Frm2":
                   
                    RemoveAllPage();
                    ChidPageFrm12 childpagefrm12 = new ChidPageFrm12();
                    AddPage(childpagefrm12, 1002);
                    SelectPage(1002);

            

                    break;

                case "Categories2Frm1":

                    RemoveAllPage();
                    ChidPageFrm21 childpagefrm21 = new ChidPageFrm21();
                    AddPage(childpagefrm21, 2001);
                    SelectPage(2001);
                

                    break;
                case "Categories2Frm2":

     
                    RemoveAllPage();
                    ChidPageFrm22 childpagefrm22 = new ChidPageFrm22();
                    AddPage(childpagefrm22, 2002);
                    SelectPage(2002);
            
               
                    break;



                default:
                    //Aside.SelectPage(Quick_usePageindex1);
                    break;
            }
        }

        private void uiSbutton_about_Click(object sender, EventArgs e)
        {
            RemoveAllPage();

            AboutFrm aboutFrm= new AboutFrm(CLoseMf);  
            AddPage(aboutFrm, 9802);
            SelectPage(9802);
            







        }

        private void uiSymbolButton1_Click(object sender, EventArgs e)
        {
           
        }

        private void 主题设置ToolStripMenuItem_Click(object sender, EventArgs e)
        {

        }
      /// <summary>
      /// 设置主题颜色并保存
      /// </summary>
      /// <param name="uistyle">需要设置的主题类型</param>
      /// <returns></returns>
        public bool UsersetUI(UIStyle uistyle)
        {
           
            Header.MenuStyle = UIMenuStyle.Black;
            try
            {
                IniFileEx iniwrite = new IniFileEx("loginset.ini");
                int style = 3;
                switch (uistyle)
                {
                    case UIStyle.Custom:
                        style = 0;
                        break;
                    case UIStyle.Blue:
                        style = 1;
                        break;
                    case UIStyle.Green:
                        style = 2;

                        break;
                    case UIStyle.Orange:
                        style = 3;
                        break;
                    case UIStyle.Red:
                        style = 4;

                        break;
                    case UIStyle.Gray:
                        style = 5;

                        break;
                    case UIStyle.Purple:
                        style = 6;

                        break;
                    case UIStyle.LayuiGreen:
                        style = 7;

                        break;
                    case UIStyle.LayuiRed:
                        style = 8;

                        break;
                    case UIStyle.LayuiOrange:
                        style = 9;

                        break;
                    case UIStyle.DarkBlue:
                        style = 101;

                        break;
                    case UIStyle.Black:
                        style = 102;

                        break;
                    case UIStyle.Colorful:
                        style = 999;

                        break;
                    default:
                        style = 3;

                        break;
                }

                uiStyleManager.Style = uistyle;
                iniwrite.Write("user", "userUI", style);
           
                return true;
            }
            catch (Exception)
            {
                UIMessageBox.Show("主题配置信息保存失败!");
                uiStyleManager.Style = UIStyle.Red;
           
                return false;
                throw;
            }
      

        }

        private void 蓝色主题ToolStripMenuItem_Click(object sender, EventArgs e)
        {
            UsersetUI(UIStyle.Blue);
        }

        private void 红色主题ToolStripMenuItem_Click(object sender, EventArgs e)
        {

            UsersetUI(UIStyle.Red);
        
        }

        private void 橙色主题ToolStripMenuItem_Click(object sender, EventArgs e)
        {
            UsersetUI(UIStyle.Orange);
        }

        private void 绿色主题ToolStripMenuItem_Click(object sender, EventArgs e)
        {
            UsersetUI(UIStyle.Green);
          
        }

        private void 深蓝主题ToolStripMenuItem_Click(object sender, EventArgs e)
        {
            UsersetUI(UIStyle.DarkBlue);
         
        }

        private void 黑色主题ToolStripMenuItem_Click(object sender, EventArgs e)
        {
            UsersetUI(UIStyle.Black);
           
        }

        private void uiContextMenuStrip2_Opening(object sender, CancelEventArgs e)
        {

        }

       /// <summary>
       /// 关闭所有uipgae
       /// </summary>
        public void RemoveAllPage()
        {        
              

            List<Control> controls= new List<Control>();
            List<UIPage> UipageList = new List<UIPage>();

            UipageList = GetPages<UIPage>();
            foreach (var item in UipageList)
            {
                RemovePage(item.PageIndex);
               
            }
            
            
        

        }

        private void 紫色主题ToolStripMenuItem_Click(object sender, EventArgs e)
        {
            UsersetUI(UIStyle.Purple);
        }

        private void 鲜红主题ToolStripMenuItem_Click(object sender, EventArgs e)
        {
            UsersetUI(UIStyle.LayuiRed);
        }
    }
}
